import { saveAs } from 'file-saver'
import {
  funDownload,
  projectDownload
} from '@/MainApp/apis/manage-design'
export interface treeNode {
  label: string
  type: string
  content: string
  children: Array<treeNode>
}
export const saveFile = (data: any, row: any) => {
  const blob = new Blob([data], {
    type: 'application/zip;'
  })
  let fileName
  if (row.scaffoldType === 'PROJECT')
    fileName = row.artifact

  if (row.scaffoldType === 'MODULE')
    fileName = row.packageName

  saveAs(blob, `${fileName}.zip`)
}
export const downloadFile = async (row: any) => {
  let data: any
  if (row.scaffoldType === 'PROJECT') {
    data = await projectDownload({
      projectId: row.id
    })
  }
  if (row.scaffoldType === 'MODULE') {
    data = await funDownload({
      moduleId: row.id,
      packageName: row.packageName
    })
  }
  if (data)
    saveFile(data, row)
}
export const downloadSingleFile = async (data: treeNode) => {
  const blob = new Blob([data.content], {
    type: `application/${data.type};`
  })
  saveAs(blob, `${data.label}`)
}
